package com.baomidou.springwind.excel;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.springwind.entity.NetworkTransportProperty;
import com.baomidou.springwind.entity.NetworkNode;
import com.baomidou.springwind.service.NetworkTransportPropertyService;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * excel文件读取
 *
 * @author luox
 */
@Component
public class TransportPropertyDataReader extends ExcelReader<NetworkTransportProperty> {

    @Autowired
    private NetworkTransportPropertyService transportService;

    @Override
    protected String saveData(List<NetworkTransportProperty> dataList, Long batchId) {
        EntityWrapper<NetworkTransportProperty> wrapper = new EntityWrapper<NetworkTransportProperty>();
        wrapper.eq("batch_id", batchId);
        deleteFlowAnalysisFile(batchId);
        transportService.delete(wrapper);//删除之前导入的数据
        for (int i=0;i<dataList.size();i++) {
            NetworkTransportProperty weekOrder=dataList.get(i);
            weekOrder.setBatchId(batchId);
            weekOrder.setCreateTime(new Date());
            weekOrder.setOperatorId(getCurrentUserId());
            if(weekOrder.getStartingPoint().isEmpty()==true||weekOrder.getGoodName().isEmpty()==true||weekOrder.getEndPoint().isEmpty()==true){
                return "上传的excel的第"+(i+2)+"行数据是空的，请填写完数据以后，重新上传";
            }
        }
        transportService.insertBatch(dataList, 100);
        return "";
    }

    private void deleteFlowAnalysisFile(Long batchId) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String path = request.getSession().getServletContext().getRealPath("");
        path = path.substring(0, path.lastIndexOf("\\"));
        path = path.substring(0, path.lastIndexOf("\\"));
        File file = new File(path + "/temp/route/" + batchId + ".obj");
        file.delete();
    }

    /**
     * 保存excel2003的数据
     * @param workbook
     * @return
     */
    @Override
    protected List<NetworkTransportProperty> verifyData(HSSFWorkbook workbook) {
        List<NetworkTransportProperty> weekOrders = new ArrayList<NetworkTransportProperty>();
        HSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        NetworkTransportProperty transportProperty;
        for (int i = 1; i <= lastRowNum; i++) {
            sheet.getRow(i);
            transportProperty = new NetworkTransportProperty();
            transportProperty.setStartingPoint(getStringCellValue(sheet.getRow(i), 0));
            transportProperty.setEndPoint(getStringCellValue(sheet.getRow(i), 1));
            transportProperty.setType(getIntCellValue(sheet.getRow(i),2));
            transportProperty.setGoodName(getStringCellValue(sheet.getRow(i), 3));
            transportProperty.setTransportRate(getdoubleCellValue(sheet.getRow(i), 4));
            transportProperty.setShippingMethod(getStringCellValue(sheet.getRow(i), 5));
            transportProperty.setTransport(getStringCellValue(sheet.getRow(i),6));
            weekOrders.add(transportProperty);
        }
        return weekOrders;
    }

    /**
     * 保存excel2007以上版本的数据
     * @param workbook
     * @return
     */
    @Override
    protected List<NetworkTransportProperty> verifyData7(XSSFWorkbook workbook) {
        List<NetworkTransportProperty> weekOrders = new ArrayList<NetworkTransportProperty>();
        XSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        NetworkTransportProperty transportProperty;
        for (int i = 1; i <= lastRowNum; i++) {
            sheet.getRow(i);
            transportProperty = new NetworkTransportProperty();
            transportProperty.setStartingPoint(getStringCellValue7(sheet.getRow(i), 0));
            transportProperty.setEndPoint(getStringCellValue7(sheet.getRow(i), 1));
            transportProperty.setType(getIntCellValue7(sheet.getRow(i),2));
            transportProperty.setGoodName(getStringCellValue7(sheet.getRow(i), 3));
            transportProperty.setTransportRate(getdoubleCellValue7(sheet.getRow(i), 4));
            transportProperty.setShippingMethod(getStringCellValue7(sheet.getRow(i), 5));
            transportProperty.setTransport(getStringCellValue7(sheet.getRow(i),6));
            weekOrders.add(transportProperty);
        }
        return weekOrders;
    }
}
